package com.example.yls.demoa.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Point;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.example.yls.demoa.R;
import com.example.yls.demoa.bean.gobang;
import com.example.yls.demoa.util.Random;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobRealTimeData;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.UpdateListener;
import cn.bmob.v3.listener.ValueEventListener;

public class MenuActivity extends Activity implements View.OnClickListener {
    private ImageView imageView;
    private Button single_game_btn;
    private Button random_game_btn;
    private Button game_setting_btn;
    private Button feedback_game_btn;
    private BmobRealTimeData rtd;
    public static int create=0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_menu);
        initView();
        setListener();
    }

    private void setListener() {
        single_game_btn.setOnClickListener(this);
        random_game_btn.setOnClickListener(this);
        game_setting_btn.setOnClickListener(this);
        feedback_game_btn.setOnClickListener(this);



    }

    private void initView() {
        imageView=findViewById(R.id.bg);
        single_game_btn=findViewById(R.id.single_game_btn);
        random_game_btn=findViewById(R.id.random_game_btn);
        game_setting_btn=findViewById(R.id.game_setting_btn);
        feedback_game_btn=findViewById(R.id.feedback_game_btn);
        Glide.with(this).load(R.drawable.menu_bg).into(imageView);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.single_game_btn:
                Intent intent=new Intent(MenuActivity.this,MainActivity.class);
                startActivity(intent);
                break;
            case R.id.random_game_btn:

                BmobQuery<gobang> bq=new BmobQuery<>();
                bq.findObjects(new FindListener<gobang>() {
                    @Override
                    public void done(List<gobang> list, BmobException e) {
                        if (e==null){
                            if (list.size()==0){
                                createRoom();
                            }else {
                                setConnectListener(list.get(Random.getRandom(list.size(),1).get(0)).getObjectId());
                            }
                        }
                    }
                });
                /*Intent intent1=new Intent(MenuActivity.this,MainActivity.class);
                intent1.putExtra("id","6BUPjjjy");
                startActivity(intent1);*/
                break;
            case R.id.game_setting_btn:
                break;
            case R.id.feedback_game_btn:
                break;
            default:
                break;
        }
    }

    /**
     * 当gobang的表为空时，创建房间
     */
    public void createRoom(){
        create=1;
        List<Point> whith=new ArrayList<>();
        gobang gb=new gobang();
        gb.setWinner(false);
        gb.setWhithPosition(whith);
        gb.setBlackPosition(whith);
        gb.setBlack(false);
        gb.save(new SaveListener<String>() {
            @Override
            public void done(String s, BmobException e) {
                if (e==null){
                    Toast.makeText(MenuActivity.this,"房间创建成功,稍后将开始比赛",Toast.LENGTH_SHORT).show();
                }else {
                    Toast.makeText(MenuActivity.this,"房间创建失败,请检查网络",Toast.LENGTH_SHORT).show();
                }
            }
        });
    }


    /**
     * 存在用户时，随机获取一个用户作为对手
     * @param objectId
     */
    public void setConnectListener(final String objectId){
        Log.e("id号码为",objectId);
        rtd = new BmobRealTimeData();
        rtd.start(new ValueEventListener() {
            @Override
            public void onDataChange(JSONObject data) {

            }

            @Override
            public void onConnectCompleted(Exception ex) {
                if (rtd.isConnected()){
                    Intent intent=new Intent(MenuActivity.this,MainActivity.class);
                    intent.putExtra("id",objectId);
                    startActivity(intent);
                }else {
                    Toast.makeText(MenuActivity.this,"加入房间失败",Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}
